package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.Calendar;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.ScanDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.ScanTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.ScanServiceInt;

/**
 * Session Bean implementation class ScanService
 * 
 * @author Patrick kendzo Email: pkendzo@msn.com
 */

@Stateless
@Remote({ ScanServiceInt.class })
public class ScanService implements ScanServiceInt {

	@SuppressWarnings("unused")
	private ScanTBL tmpScan;
	@EJB
	private ScanDAOInt formDao;

	@EJB
	private CustomerServiceInt cust;

	/**
	 * Default constructor.
	 */
	public ScanService() {
		tmpScan = new ScanTBL();
	}

	@Override
	public int deleteScanItem(Long id) {
		this.formDao.delete(id);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getA_instance(ScanTBL instance) {
		this.tmpScan = instance;

	}

	@Override
	public int createScanItem(String matricule, Calendar datum,
			String dateiName, String bezeichner, String url,
			String actualFomularNumber, int typ) {
		ScanTBL tmpForm_1 = new ScanTBL();

		tmpForm_1.setCustomer(cust.getCustomer(matricule));
		tmpForm_1.setDatum(datum);
		tmpForm_1.setDateiName(dateiName);
		tmpForm_1.setBezeichner(bezeichner);
		tmpForm_1.setSpeicherOrt(url);
		tmpForm_1.setTyp(typ);

		this.formDao.persist(tmpForm_1);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public ScanTBL[] getScanBYCustomer(String matricule) {
		Object[] tmp = formDao.retrieveScanItemsByCustomer(matricule).toArray();
		ScanTBL[] eintraege = new ScanTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (ScanTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public ScanTBL[] getScanBY_Typ(String typ) {
		Object[] tmp = formDao.retrieveScanBY_Typ(typ).toArray();
		ScanTBL[] eintraege = new ScanTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (ScanTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public ScanTBL[] getScan_BYCustomer_and_BYtyp(String matricule, String typ) {
		Object[] tmp = formDao
				.retrieveScan_BYCustomer_and_BYtyp(matricule, typ).toArray();
		ScanTBL[] eintraege = new ScanTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (ScanTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public Long getSubPrimaryKeyIndexBYTyp(int typ) {
		return formDao.findSubPrimaryKeyIndexBYTyp(typ);
	}

	@Override
	public void update_bezeichner(Long id, String bezeichner) {
		ScanTBL tmpScan_1 = this.formDao.retrieveScanItemByID(id);
		tmpScan_1.setBezeichner(bezeichner);
		this.formDao.update(tmpScan_1);

	}

	@Override
	public void update_date(Long id, Calendar datum) {
		ScanTBL tmpScan_1 = this.formDao.retrieveScanItemByID(id);
		tmpScan_1.setDatum(datum);
		this.formDao.update(tmpScan_1);

	}

}
